from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QVBoxLayout, QWidget
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QFontMetrics


class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.label = QLabel(self)
        self.label.setText("你好nihaonihaonihaonihao")
        

        # 获取QLabel的初始文本
        newStr = self.label.text()

        # 获取QLabel使用的字体度量
        fontWidth = QFontMetrics(self.label.font())

        # 计算需要省略的文本
        elideNote = fontWidth.elidedText(newStr, Qt.ElideRight, self.label.width())

        # 设置文本和工具提示
        self.label.setText(elideNote)
        self.label.setToolTip(newStr)


if __name__ == "__main__":
    import sys

    app = QApplication(sys.argv)

    window = MainWindow()
    window.show()

    sys.exit(app.exec_())
